package data;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class Card {
	public int level = 0;
	public int hp = 0;
	public int at = 0;
	public int minhp = 0;
	public int minat = 0;
	public int maxhp = 0;
	public int maxat = 0;
	public int exp = 0;
	public int limit = 100;
	public int rank = 0;
	public int skill = 0;
	public String name = "";
	public int step = 0;
	public int ap = 3;
	public static final int U = 5, M = 4, S = 3, R = 2, N = 1, C = 0;
	public Card(String name, int rank, int skill) {
		this.name = name;
		this.rank = rank;
		this.skill = skill;
		switch (rank) {
		case U: step = 10;break;
		case M: step =  4;break;
		case S: step =  7;break;
		case R: step = 10;break;
		case N: step = 14;break;
		default:step =  0;break;
		}
	}
	public static Card init(String name, int rank) {
		return new Card(name, rank, 0);
	}
	public static Card init(String name, int rank, int skill) {
		return new Card(name, rank, skill);
	}
	public static Vector<Card> getSample() {
		Card card;
		Vector<Card> list = new Vector<Card>();
		list.add(init("TestCard U", U));
		list.add(init("TestCard M", M));
		list.add(init("TestCard S", S));
		list.add(init("TestCard R", R));
		list.add(init("TestCard N", N));
		
		list.add(init("TestCard C1", C));
		card = list.lastElement();
		card.minhp = 3000;
		card.minat = 3000;
		card.maxhp = 15000;
		card.maxat = 15000;
		card.level = 100;
		card.limit = 80;
		card.levelUp();
		card.skill = 0;
		
		list.add(init("TestCard C2", C));
		card = list.lastElement();
		card.minhp = 2400;
		card.minat = 2400;
		card.maxhp = 12000;
		card.maxat = 12000;
		card.level = 100;
		card.limit = 90;
		card.levelUp();
		card.skill = 1;
		return list;
	}
	public static Card getUnique() {
		/* Default Card */
		Connection connex = Database.load("static.db");
		Card card = null;
		Statement stmt = null;
		ResultSet rst = null;
		try {
			stmt = connex.createStatement();
			rst = stmt.executeQuery( "SELECT * FROM CARDINFO WHERE ID=1");
			if (rst.next()){	// If info exists
				card = init(rst.getString("NAME"), rst.getInt("RANK"));
				card.minhp = rst.getInt("MINHP");
				card.minat = rst.getInt("MINAT");
				card.maxhp = rst.getInt("MAXHP");
				card.maxat = rst.getInt("MAXAT");
				card.level = 1;
				card.limit = rst.getInt("LIMIT");
				card.skill = rst.getInt("SKILL");
				rst.close();
				stmt.close();
				connex.close();
			} else {
				System.out.println("Unable to request the initial card");
				connex.close();
				System.exit(0);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return card;
	}
	public void unlock(int depth, int step) {
		limit += step*depth;
		if (limit > 100) limit = 100;
		levelUp();
	}
	public void levelUp() {
		hp = minhp+(maxhp - minhp)*level*limit/10000;
		at = minat+(maxat - minat)*level*limit/10000;
	}
	private void convert() {
		
	}
}
